package com.changgou.search.controller;

import com.changgou.search.pojo.SkuInfo;
import com.changgou.search.feign.SkuInfoFeign;
import entity.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;
import java.util.Set;

/**
 * @Author: wzw
 * @Date: 2020/12/17 22:20
 * @version: 1.8
 */
@Controller
@RequestMapping("/search")
public class SearchController {

    //注入SkuIofoFeign
    @Autowired
    private SkuInfoFeign skuInfoFeign;

    /**
     * @author wzw
     * 搜索页面回显数据
     * @Date 22:22 2020/12/17
     * @param
     * @return java.lang.String
    **/
    @GetMapping("/list")
    public String list(@RequestParam(required = false)Map<String,String> searchMap, Model model){
        //替换特殊字符
        handlerSearchMap(searchMap);

        //实现功能:调用搜索服务(检索功能)
        Map<String, Object> resultMap = skuInfoFeign.search(searchMap);
        //将检索条件放入model中
        model.addAttribute("searchMap",searchMap);
        //将检索到的数据放入model中
        model.addAttribute("resultMap", resultMap);

        //===调用自定义方法:组装url===
        String url = getUrl(searchMap);
        //将url添加到model中
        model.addAttribute("url", url);

        //===构造前端需要的分页对象==
        //获取总条数
        long totalElements = Long.parseLong(resultMap.get("totalElements").toString());
        //获取当前页码
        int pageNum = Integer.parseInt(resultMap.get("pageNum").toString());
        //获取每页数量
        int pageSize = Integer.parseInt(resultMap.get("pageSize").toString());
        //实现功能:调用工具类实现分页功能
        Page<SkuInfo> page = new Page<>(totalElements, pageNum, pageSize);
        //添加到model中
        model.addAttribute("page", page);

        //返回搜索页面:视图地址
        return "search";
    }

    /**
     * @author wzw
     * 替换特殊字符
     * @Date 22:26 2020/12/18
     * @param searchMap 替换的参数
     * @return void
    **/
    private void handlerSearchMap(Map<String, String> searchMap) {
        //非空判断
        if (searchMap != null) {
            //将map转为set并且遍历
            for (Map.Entry<String, String> entry : searchMap.entrySet()) {
                //判断前缀确定是规格参数
                if (entry.getKey().startsWith("spec_")) {
                    //替换参数
                    entry.setValue(entry.getValue().replace("+", "%2B"));
                }

            }
        }
    }

    private String getUrl(Map<String, String> searchMap) {
        // 例：/search/list?keywords=黑马&brand=小米
        //获取映射路径
        String url = "/search/list";
        //非空判断
        if (searchMap != null && searchMap.size() > 0) {
            ///search/list?
            //第一次拼接要以?开始,后面采用&
            url += "?";
            //将map转换为set
            Set<Map.Entry<String, String>> entries = searchMap.entrySet();
            //遍历set集合
            for (Map.Entry<String, String> entry : entries) {
                //获取条件字段key
                String key = entry.getKey();
                //获取条件字段value
                String value = entry.getValue();

                //判断是否是分页条件
                if (key.equals("pageNum")) {
                    //前端实现分页,无需拼接,直接跳出循环
                    continue;
                }

                //拼接条件
                //es:/search/list?keywords=黑马&brand=小米&
                url += key + "=" +value + "&";
            }
            //截取字符窜:去掉最后一个&
            //es:/search/list?keywords=黑马&brand=小米
            url = url.substring(0, url.length() - 1);
        }
        //返回拼接完的结果
        return url;
    }


}
